package com.zy.mc_module.ui.micang;

import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zy.mc_module.base.kotlin.BaseActivity;
import com.zy.mc_module.databinding.ActivityRiceBinding;
import com.zy.mc_module.mvp.bean.UserBean;
import com.zy.mc_module.mvp.bean.UserTaskCount;
import com.zy.mc_module.ui.micang.setting.MiCangSettingActivity;
import com.zy.mc_module.utils.AdRewardUtil;
import com.zy.mc_module.utils.AppTools;
import com.zy.mc_module.utils.GlideUtil;
import com.zy.mc_module.utils.StatusBarUtil;
import com.zy.mc_module.utils.retrofit.error.ThdException;

/**
 * 米仓中心
 */
public class RiceActivity extends BaseActivity<RiceContract.View, RicePresenter, ActivityRiceBinding> implements RiceContract.View {

    private UserBean.UserMemberInfo memberInfo;

    @NonNull
    @Override
    public ActivityRiceBinding getViewBinding() {
        return ActivityRiceBinding.inflate(getLayoutInflater());
    }

    @Nullable
    @Override
    public RicePresenter createPresenter() {
        return new RicePresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        StatusBarUtil.setImmersiveStatusBar(this);

        initData();
    }

    private void initData() {
        getPresenter().getUserInfo(context);
        getPresenter().userTaskCount(context);
    }

    @Override
    protected void initListener() {
        super.initListener();
        binding.viewTitle.back.setOnClickListener(v -> finish());
        binding.activityRiceLvStar.setOnClickListener(v -> {
            startActivity(new Intent(getContext(), StarPersonActivity.class));
        });
        binding.activityRiceMessage.setOnClickListener(view -> {
            startActivity(new Intent(getContext(), RiceMessageActivity.class));
        });
        binding.clTaskToday.setOnClickListener(view -> {
            AdRewardUtil.getInstance().start(RiceActivity.this, "2", "1-test-extraInfo");
        });
        binding.clRiceStorehouse.setOnClickListener(v -> {
            startActivity(new Intent(getContext(), MineRiceActivity.class));
        });
        binding.clRiceGrain.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), RiceDetailActivity.class);
            if (memberInfo != null)
                intent.putExtra("MineRice", memberInfo.getRiceGrains());
            startActivity(intent);
        });
        binding.clRiceActivity.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), MineActiveActivity.class);
            if (memberInfo != null)
                intent.putExtra("MineActive", memberInfo.getActive());
            startActivity(intent);
        });
        binding.clRiceExp.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), MemberConfigActivity.class);
            if (memberInfo != null) {
                intent.putExtra("MineLevel", memberInfo.getMemberLevel());
                intent.putExtra("MineExp",memberInfo.getExperience());
            }
            startActivity(intent);
        });
        binding.clMineTeam.setOnClickListener(v -> {
            startActivity(new Intent(getContext(), MineTeamActivity.class));
        });
        binding.clInvite.setOnClickListener(v -> {
            startActivity(new Intent(getContext(), InviteFriendActivity.class));
        });
        binding.clSetting.setOnClickListener(v -> {
            startActivity(new Intent(getContext(), MiCangSettingActivity.class));
        });
    }

    @Override
    public void onError(ThdException error) {

    }

    @Override
    public <T> void onSuccess(T t, int type) {
        if (type == RicePresenter.USER_MEMBER_INFO) {
            memberInfo = (UserBean.UserMemberInfo) t;
            GlideUtil.displayImage(binding.activityRiceUserHead, memberInfo.getHeadImage());
            binding.activityRiceUserPhone.setText(memberInfo.getNickname());
            binding.activityRiceUserLv.setText((memberInfo.getMemberLevel()));
            binding.riceStorehouse.setText(memberInfo.getRiceWarehouse());
            binding.riceGrain.setText(AppTools.decimalsT(memberInfo.getRiceGrains()) + "点");
            binding.riceExp.setText(memberInfo.getExperience() + "点");
            binding.riceActivity.setText(AppTools.decimalsT(memberInfo.getActive()) + "点");
        } else if (type == RicePresenter.USER_TASK_COUNT) {
            UserTaskCount taskCount = (UserTaskCount) t;
            binding.todayTask.setText("今日任务" + taskCount.getTodayTaskCount() + "/" + taskCount.getTaskCount());
        }
    }

    @Override
    public void onError(ThdException error, int type) {

    }
}
